﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using System;
using System.Diagnostics;
using System.Globalization;

namespace UtilLib
{
    /// <summary>
    /// 公共函数类  时间部分 
    /// </summary>
    public static partial class Util
    {
        #region 调试处理时间

        /// <summary>
        /// 处理时间显示
        /// </summary>
        public static Stopwatch BenchmarkStopwatch;
        /// <summary>
        /// 处理对象
        /// </summary>
        public static string BenchmarkMode = "";
        /// <summary>
        /// 开始处理
        /// </summary>
        /// <param name="mode">处理对象</param>
        public static void BenchmarkStart(string mode = "执行时间")
        {
            BenchmarkMode = mode;
            Show($"处理 <{BenchmarkMode}>。。。。。。开始。。。。。。");
            BenchmarkStopwatch = Stopwatch.StartNew();
        }
        /// <summary>
        /// 结束处理
        /// </summary>
        public static void BenchmarkEnd()
        {
            if (BenchmarkStopwatch == null)
            {
                return;
            }
            BenchmarkStopwatch.Stop();
            Show($"处理 <{BenchmarkMode}>。。。。。。完毕，耗时 {BenchmarkStopwatch.ElapsedMilliseconds} 毫秒 。");
        }
        /// <summary>
        /// 用来测试执行时间
        /// </summary>
        /// <param name="warmupIterations">热身次数</param>
        /// <param name="benchmarkIterations">执行次数</param>
        /// <param name="action"></param>
        public static void RunBenchmark(int warmupIterations, int benchmarkIterations, Action action)
        {
            Console.WriteLine("Warmup iterations: {0}", warmupIterations);
            for (int i = 0; i < warmupIterations; i++)
            {
                action();
            }

            Console.WriteLine("Benchmark iterations: {0}", benchmarkIterations);
            var stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < benchmarkIterations; i++)
            {
                action();
            }
            stopwatch.Stop();
            Console.WriteLine("Elapsed time: {0}ms", stopwatch.ElapsedMilliseconds);
            Console.WriteLine("Ops per second: {0}", (int)((double)benchmarkIterations * 1000 / stopwatch.ElapsedMilliseconds));
        }

        #endregion

    }
}